package com.example.androidclassseven;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageButton imageButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        setSupportActionBar(toolBar);

        TextView tvShow = findViewById(R.id.textView);
        imageButton = findViewById(R.id.imageButton);
        registerForContextMenu(tvShow);

        Log.i("TAG", "onCreate: ");

    }

    //渲染上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        getMenuInflater().inflate(R.menu.menu_context, menu);
    }

    //上下文菜单
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.context_edit:
                Toast.makeText(MainActivity.this, "context_edit", Toast.LENGTH_SHORT).show();
                break;
            case R.id.context_copy:
                Toast.makeText(MainActivity.this, "context_copy", Toast.LENGTH_SHORT).show();
                break;
            case R.id.context_paste:
                Toast.makeText(MainActivity.this, "context_paste", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

    //渲染菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_item, menu);
        return true;
    }

    //菜单选项
//    @Override
//    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//        switch (item.getItemId()){
//            case R.id.action_setting:
//                Toast.makeText(MainActivity.this, "settingClick", Toast.LENGTH_SHORT).show();
//                break;
//            case R.id.action_write:
//                Toast.makeText(MainActivity.this, "writeClick", Toast.LENGTH_SHORT).show();
//                break;
//        }
//        return super.onOptionsItemSelected(item);
//    }

    public void settingClick(MenuItem item) {
        Toast.makeText(MainActivity.this, "settingClick", Toast.LENGTH_SHORT).show();
    }

    public void writeClick(MenuItem item) {
        Toast.makeText(MainActivity.this, "writeClick", Toast.LENGTH_SHORT).show();
    }

    //弹出菜单
    public void showPopMenu(View view) {
        PopupMenu popupMenu = new PopupMenu(this, imageButton);
        popupMenu.getMenuInflater().inflate(R.menu.menu_pop, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.menu_reply:
                    Toast.makeText(getApplicationContext(), "menu_reply", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.menu_share:
                    Toast.makeText(MainActivity.this, "menu_share", Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        });
        popupMenu.show();
    }

    //警报弹窗
    public void showAlert(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("我是警报弹窗").setMessage("111");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNeutralButton("再说", (dialogInterface, i) -> Toast.makeText(MainActivity.this, "再说", Toast.LENGTH_SHORT).show());
        builder.create().show();
    }

    public void showetDate(View view) {
//        new MyDataDialog().show(getSupportFragmentManager(),"dataPiker");
        new MyTimeDialog().show(getSupportFragmentManager(),"timePiker");
    }
}